package com.gill.demo;

import org.apache.zookeeper.*;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @Description: zookeeper 获取子节点数据的demo演示
 */
public class ZKNodeQueryChildList implements Watcher {

	private ZooKeeper zookeeper = null;
	
	public static final String zkServerPath = "env:2181";
	public static final Integer timeout = 5000;
	
	public ZKNodeQueryChildList() {}
	
	public ZKNodeQueryChildList(String connectString) {
		try {
			zookeeper = new ZooKeeper(connectString, timeout, new ZKNodeQueryChildList());
		} catch (IOException e) {
			e.printStackTrace();
			if (zookeeper != null) {
				try {
					zookeeper.close();
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
	
	private static CountDownLatch countDown = new CountDownLatch(1);
	
	public static void main(String[] args) throws Exception {

		// 建立连接，创建ZK客户端
		ZKNodeQueryChildList zkServer = new ZKNodeQueryChildList(zkServerPath);
		
		/**
		 * 参数：
		 * path：父节点路径
		 * watch：true或者false，注册一个watch事件
		 */
		// 同步调用
//		List<String> strChildList = zkServer.getZookeeper().getChildren("/imooc", true);
//		for (String s : strChildList) {
//			System.out.println(s);
//		}
		
		// 异步调用
		String ctx = "{'callback':'ChildrenCallback'}";
//		zkServer.getZookeeper().getChildren("/imooc", true, new Children1Callback(), ctx);
		zkServer.getZookeeper().getChildren("/imooc", true, new Children2Callback(), ctx);
		countDown.await();
	}
	
	@Override
	public void process(WatchedEvent event) {
		try {
			if(event.getType()== EventType.NodeChildrenChanged){//针对的是对子节点的删除、创建事件，修改子节点数据不会触发该事件
				System.out.println("NodeChildrenChanged");
				ZKNodeQueryChildList zkServer = new ZKNodeQueryChildList(zkServerPath);
				List<String> strChildList = zkServer.getZookeeper().getChildren(event.getPath(), false);
				for (String s : strChildList) {
					System.out.println(s);
				}
				countDown.countDown();
				// 以下3种情况都是针对父节点/imooc
			} else if(event.getType() == EventType.NodeCreated) {
				System.out.println("NodeCreated");
			} else if(event.getType() == EventType.NodeDataChanged) {
				System.out.println("NodeDataChanged");
			} else if(event.getType() == EventType.NodeDeleted) {
				System.out.println("NodeDeleted");
			} 
		} catch (KeeperException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public ZooKeeper getZookeeper() {
		return zookeeper;
	}
	public void setZookeeper(ZooKeeper zookeeper) {
		this.zookeeper = zookeeper;
	}

	/**
	 * 回调接口的实现类
	 */
	private static class Children1Callback implements AsyncCallback.ChildrenCallback {

		@Override
		public void processResult(int rc, String path, Object ctx, List<String> children) {
			System.out.println("子节点变化:"+children);
			System.out.println("ChildrenCallback:" + path);
			System.out.println((String)ctx);
		}
	}

	/**
	 * 回调接口的实现类
	 */
	private static class Children2Callback implements AsyncCallback.Children2Callback{

		@Override
		public void processResult(int rc, String path, Object ctx, List<String> children,Stat stat) {
			System.out.println("子节点变化:"+children);
			System.out.println("ChildrenCallback:" + path);
			System.out.println((String)ctx);
			System.out.println("节点信息:"+stat.toString());
		}
	}
}

